package com.mitac.mcd.cs.mobiletracking.socketserver.util;

import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.mitac.mcd.cs.mobiletracking.socketserver.MobileServer;

public class SendHTMLMail {

	public static void sendMail(String title,String tcontent){
		String tto = MobileServer.ConfigProperties.getProperty("SYSTEM_ADMIN_EMAILS");
		String ttitle = "Socket Server Log Daily Report "+"("+Formatter.daily.format(new Date())+")";
		Properties props = new Properties();
		props.put("mail.smtp.host",MobileServer.ConfigProperties.getProperty("mail.smtp.host"));
		props.put("mail.smtp.auth",MobileServer.ConfigProperties.getProperty("mail.smtp.auth"));
		Session s = Session.getInstance(props);
		s.setDebug(true);
		
		MimeMessage message=new MimeMessage(s);
		//给消息对象设置发件人/收件人/主题/发信时间
		try{
		  InternetAddress from = new InternetAddress(MobileServer.ConfigProperties.getProperty("mailfrom"));
		  message.setFrom(from);
		  
		  String[] arrayTo = null;
		  if(tto!=null&&!tto.equals("")){
			  if(tto.indexOf(",")>0){
				  arrayTo = tto.split(",");
			  }else{
				  arrayTo = new String[]{tto};
			  }
		  }
		  InternetAddress[] to = new InternetAddress[arrayTo.length];
		  for(int i=0;i<arrayTo.length;i++){
			  to[i] = new InternetAddress(arrayTo[i]);
		  }
		  message.setRecipients(Message.RecipientType.TO,to);
		  message.setSubject(ttitle);
		  message.setSentDate(new Date());


		  //给消息对象设置内容
		  BodyPart mdp = new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
		  mdp.setContent(tcontent,"text/html;charset=UTF-8");//给BodyPart对象设置内容和格式/编码方式
		  Multipart mm = new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对
		  //象(事实上可以存放多个)
		  mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
		  message.setContent(mm);//把mm作为消息对象的内容
		  
		  BodyPart upbp = new MimeBodyPart();
		  BodyPart downbp = new MimeBodyPart();
		  File upfile = new File("./img/up.jpg");
		  File downfile = new File("./img/down.jpg");
		  FileDataSource upFileds = new FileDataSource(upfile);
		  FileDataSource downFileds = new FileDataSource(downfile);
		  upbp.setDataHandler(new DataHandler(upFileds));
		  downbp.setDataHandler(new DataHandler(downFileds));
		  upbp.setFileName(upfile.getName());
		  downbp.setFileName(downfile.getName());
		  upbp.setHeader("content-id", upfile.getName());
		  downbp.setHeader("content-id", downfile.getName());
		  // 添加附件
		  mm.addBodyPart(upbp);
		  mm.addBodyPart(downbp);
		  
		  message.saveChanges();
		  Transport transport=s.getTransport("smtp");
		  transport.connect("smtp.163.com",
				  MobileServer.ConfigProperties.getProperty("mailusername"),
				  MobileServer.ConfigProperties.getProperty("mailpassword"));
		  transport.sendMessage(message,message.getAllRecipients());
		  transport.close();
		  System.out.println("邮件发送成功!");
		}catch (Exception e) {
			e.printStackTrace();
			BusinessUtil.aLogger.error("sendMail:"+e);
		}
	}
	
	public static void main(String args[]){
		String tcontent="<table border='1' bgColor='red'><tr><td>发送一个表格<img src=\"cid:up.jpg\" /></td></tr></table>";//HTML代码
		String title = "Socket Server Log Daily Report "+"("+Formatter.daily.format(new Date())+")";
		sendMail(title,tcontent);
	}
	
}
